<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

       <el-form-item label="用户手机号" prop="userPhone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:order:edit']"
        >修改</el-button>
      </el-col> -->
      
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="清洗店铺" align="center" prop="washShop" />
      <el-table-column label="清洗的工厂" align="center" prop="washFactory" />
      <el-table-column label="客户的电话" align="center" prop="customPhone" />
      <el-table-column label="订单来源" align="center" prop="comeFrom" >
       <template slot-scope="scope">
          {{ orderComeFromMap[scope.row.comeFrom] }}
       </template>
     </el-table-column>
      <el-table-column label="预约日期" align="center" prop="mtBeginTime" width="150px"/>
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="工单状态" align="center" prop="status" >
        <template slot-scope="scope">
            {{scope.row.status==0? '进行中':'已完结'}}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" >
          <el-button
            size="mini"
            type="text"
            @click="addNodes(scope.row)"
            v-if="scope.row.status==0"
          >增加节点</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status==0"
            @click="handlefinish(scope.row)"
          >完结工单</el-button>
         
         <el-button
            size="mini"
            type="text"
            v-if="scope.row.status==1"
            @click="addNodes(scope.row)"
          >查看</el-button> 


        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <createAndUpdateWorkOrder  :shopListMap="{}" :rollBackFn="getList" :details="details" :showWorkOrder="showWorkOrder" :workNumber="workNumber"    @cancel-workOrder-click="cancelWorkOrder"></createAndUpdateWorkOrder>
  </div>
</template>

<script>
import { list , updateStatus} from "@/api/workOrder/index";
import { orderComeFromMap } from  '@/config/index'
import createAndUpdateWorkOrder from "../order/components/createAndUpdateWorkOrder.vue";
export default {
  name: "Order",
  components:{
    createAndUpdateWorkOrder
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      orderComeFromMap,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      workNumber:"444",
      details:{},
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      showWorkOrder:false,
      // 工单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderNo: null,
        shopId: null,
        factoryId: null,
        washFactory: null,
        washShop: null,
        customPhone: null,
        comeFrom: null,
        mtBeginTime: null,
        totalPrice: null,
        status: null,
        goodsName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        comeFrom: [
          { required: true, message: "订单来源 1pc  2美团  3抖音 4小程序不能为空", trigger: "blur" }
        ],
        totalPrice: [
          { required: true, message: "总价 = 订单金额 - 优惠价格不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "工单状态 0 1已完结不能为空", trigger: "change" }
        ],
        goodsName: [
          { required: true, message: "商品名字不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handlefinish(item){
        updateStatus({workOrderId:item.id}).then(res=>{
            this.getList()
        })
      

    },

    cancelWorkOrder(){
        this.showWorkOrder=false
    },

    addNodes(item){
     this.showWorkOrder=true
     this.workNumber=item.id
    //  item.id=item.orderId
     this.details=item
     

    },
    
    /** 查询工单列表 */
    getList() {
      this.showWorkOrder=false
      this.loading = true;
      list(this.queryParams).then(response => {
        this.orderList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        orderNo: null,
        shopId: null,
        factoryId: null,
        washFactory: null,
        washShop: null,
        customPhone: null,
        comeFrom: null,
        mtBeginTime: null,
        totalPrice: null,
        status: null,
        goodsName: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
